package com.itheima.controller;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.OperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 操作日志控制器
 */
@Slf4j
@RestController
@RequestMapping("/operateLog")
public class OperateLogController {
    
    @Autowired
    private OperateLogService operateLogService;
    
    /**
     * 分页查询操作日志
     */
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer pageSize,
                      Integer operateEmpId,
                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startDate,
                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endDate,
                      String className,
                      String methodName) {
        log.info("分页查询操作日志, 参数: page={}, pageSize={}, operateEmpId={}, startDate={}, endDate={}, className={}, methodName={}", 
                page, pageSize, operateEmpId, startDate, endDate, className, methodName);
        
        PageBean pageBean = operateLogService.page(page, pageSize, operateEmpId, startDate, endDate, className, methodName);
        return Result.success(pageBean);
    }
    
    /**
     * 根据ID删除操作日志
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除操作日志, id: {}", id);
        operateLogService.deleteById(id);
        return Result.success();
    }
    
    /**
     * 批量删除操作日志
     */
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        log.info("批量删除操作日志, ids: {}", ids);
        operateLogService.deleteByIds(ids);
        return Result.success();
    }
    
    /**
     * 删除所有操作日志
     */
    @DeleteMapping("/all")
    public Result deleteAll() {
        log.info("删除所有操作日志");
        operateLogService.deleteAll();
        return Result.success();
    }
    
    /**
     * 获取统计信息
     */
    @GetMapping("/statistics")
    public Result getStatistics() {
        log.info("获取操作日志统计信息");
        Map<String, Object> statistics = operateLogService.getStatistics();
        return Result.success(statistics);
    }
    
    /**
     * 获取趋势数据
     */
    @GetMapping("/trend")
    public Result getTrendData() {
        log.info("获取操作日志趋势数据");
        List<Map<String, Object>> trendData = operateLogService.getTrendData();
        return Result.success(trendData);
    }
    
    /**
     * 获取类型分布数据
     */
    @GetMapping("/type")
    public Result getTypeData() {
        log.info("获取操作日志类型分布数据");
        List<Map<String, Object>> typeData = operateLogService.getTypeData();
        return Result.success(typeData);
    }
}
